package com.groupbuying.redpacket.controller;


import com.groupbuying.redpacket.dto.RedPacketTaskDTO;
import com.groupbuying.redpacket.dto.RedPacketTaskVo;
import com.groupbuying.redpacket.service.RedPacketTaskService;
import com.groupbuying.redpacket.utils.PageUtils;
import com.groupbuying.redpacket.utils.Query;
import com.groupbuying.redpacket.utils.apiresult.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task")
public class RedPacketTaskController {
    @Autowired
    private RedPacketTaskService redPacketTaskService;


    /**
     * 增加红包任务
     * @param redPacketTaskDTO
     * @return ApiResult
     */
    @PostMapping("/addTaskAndRule")
    public ApiResult addTaskAndRule(@RequestBody RedPacketTaskDTO redPacketTaskDTO){
        return redPacketTaskService.addTaskAndRule(redPacketTaskDTO,redPacketTaskDTO.getRedPacketRuleDOS());
    }

    /**
     * 修改红包任务
     * @param redPacketTaskDTO
     * @return ApiResult
     */
    @PostMapping("/updateTaskAndRule")
    public ApiResult updateTaskAndRule(@RequestBody RedPacketTaskDTO redPacketTaskDTO){
        return redPacketTaskService.updateTaskAndRule(redPacketTaskDTO,redPacketTaskDTO.getRedPacketRuleDOS());
    }

    /**
    * @api {post} /task/getLargeScreenState 获取红包任务状态
    * @apiDescription 大屏出现问题，本方法提供当前任务状态
    * @apiGroup redPacket
    * @apiHeader {String} Authorization 用户授权token
    * @apiSuccess {JSON} String {"msg": "请求成功","result": "0","code": 1} result 状态：0：初始页面 1：倒计时 2：活动进行中 3：结算页
    * @author srlg
    * @date 2019-11-25
    * @apiVersion 1.0.0
    * @tar 红包大屏
    */
    @GetMapping("/getLargeScreenState")
    public ApiResult getLargeScreenState(){
        return redPacketTaskService.nowTaskStateService();
    }


    /**
     * 查询红包任务
     * @return PageUtils
     */
    @GetMapping("getListTask")
    public PageUtils getListTask(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        List<RedPacketTaskVo> redPacketTaskVos = redPacketTaskService.listTask(query);
        int total = redPacketTaskService.getCount();
        PageUtils pageUtils = new PageUtils(redPacketTaskVos,total);
        return pageUtils;
    }

    /**
     * 根据id查询任务
     * @param id
     * @return
     */
    @GetMapping("getTask")
    public ApiResult getTask(@RequestParam("id") Long id){
        return redPacketTaskService.getTask(id);
    }
}
